# gozerlib/persist/pdod.py
#
#

""" pickled dicts of dicts """

from persist import Persist
 
class Pdod(Persist):

    """
        pickled dicts of dicts.

        :param filename: filename to store data in
        :type filename: string
        :param default: data to initialise pdod with
        :type default: dict, list , string, integer or None

    """

    def __init__(self, filename, default={}):
        Persist.__init__(self, filename)

        if not self.data:
            self.data = {}

    def __getitem__(self, name):

        """ return item with name. """

        if self.data.has_key(name):
            return self.data[name]

    def save(self):

        """ save data to database. """

        Persist.save(self)

    def __delitem__(self, name):

        """
            delete name item. 

            :param name: name of item to delete
            :type name: string

        """

        if self.data.has_key(name):
            return self.data.__delitem__(name)

    def __setitem__(self, name, item):

        """
             set name item. 

            :param name: name of item to set
            :type name: string
            :param item: data to set
            :type item: dict, list, string, integer or None
            :rtype: None

        """

        self.data[name] = item

    def __contains__(self, name):

        """ 
            check if data is available. 

            :param name: key to check for
            :type name: string
            :rtype: boolean

        """

        return self.data.__contains__(name)

    def setdefault(self, name, default):

        """
            set default of name.

            :param name: name of item to set default for
            :type name: string
            :param default: data to set
            :type default: dict, list, string, integer or None
            :rtype: dict, list, string, integer or None
            
        """

        return self.data.setdefault(name, default)

    def has_key(self, name):

        """
            has name key. 

            :param name: key name
            :type name: string
            :rtype: boolean
 
        """

        return self.data.has_key(name)

    def has_key2(self, name1, name2):

        """
            has [name1][name2] key. 

            :param name1: first key name
            :type name1: string
            :param name2: second key name
            :type name3: string
            :rtype: boolean

        """

        if self.data.has_key(name1):
            return self.data[name1].has_key(name2)

    def get(self, name1, name2):

        """
            get data[name1][name2].

            :param name1: first key
            :type name1: string
            :param name2: second key
            :type name2: string
            :rtype: dict, list, string, integer or None

        """

        try:
            result = self.data[name1][name2]
            return result
        except KeyError:
            return None

    def set(self, name1, name2, item):

        """
            set name, name2 item 

            :param name1: first key
            :type name1: string
            :param name2: second key
            :type name2: string
            :param item: data to set
            :type item: dict, list, string, integer or None
            :rtype: None

        """

        if not self.data.has_key(name1):
            self.data[name1] = {}

        self.data[name1][name2] = item
